﻿using DG.Tweening;
using QFramework;
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Object = UnityEngine.Object;

namespace HareGame
{
    public partial class Util
    {
        private static Util _instance;
        public static Util Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new Util();
                    _loader = ResLoader.Allocate();
                }

                return _instance;
            }
        }

        private static ResLoader _loader;
        private static Dictionary<string, Object> _cache = new Dictionary<string, Object>();

        public T LoadAsset<T>(string assetName) where T : Object
        {
            if (_cache.ContainsKey(assetName))
                return (T)_cache[assetName];

            T t = _loader.LoadSync<T>(assetName);
            if (!_cache.ContainsKey(assetName))
                _cache.Add(assetName, t);

            return t;
        }
    }

    public static class UtilExtension
    {
        public static string double2String(this double value)
        {
            return GameEntry.Util.Num2String(value);
        }

        public static string float2String(this float value)
        {
            return GameEntry.Util.Num2String(value);
        }

        public static string int2String(this int value)
        {
            return GameEntry.Util.Num2String(value);
        }

        public static T string2Enum<T>(this string enumName)
        {
            return (T)Enum.Parse(typeof(T), enumName);
        }

        public static Tweener Delay2Do(this object obj, float delay, TweenCallback callback)
        {
            return DOTween.To(() => delay, x => delay = x, 0, delay).OnComplete(callback);
        }

        public static T GetOrAddComponent<T>(this GameObject obj) where T : Component
        {
            T component;
            if (obj.GetComponent<T>() != null)
            {
                component = obj.GetComponent<T>();
            }
            else
            {
                component = obj.AddComponent<T>();
            }

            return component;
        }

        /// <summary>
        /// 设置为多语言文本
        /// </summary>
        /// <param name="key">key</param>
        /// <returns></returns>
        public static string AsLocalization(this string key)
        {
            return GameEntry.Localization.GetValue(key);
        }

        /// <summary>
        /// 设置Icon
        /// </summary>
        /// <param name="image">图片组建</param>
        /// <param name="atlasType">所属图集</param>
        /// <param name="iconName">icon文件名</param>
        public static void SetIcon(this Image image, AtlasType atlasType, string iconName)
        {
            image.sprite = GameEntry.Atlas.LoadSprite(atlasType, iconName);
            image.SetNativeSize();
        }

        /// <summary>
        /// 图片至灰
        /// </summary>
        /// <param name="image">图片组建</param>
        /// <param name="needAll">是否全灰</param>
        public static void SetGray(this Image image, bool needAll = false)
        {
            Material _mat = null;
            if (needAll)
            {
                _mat = GameEntry.Util.LoadAsset<Material>(QAssetBundle.Mat_uigrayonly_mat.Mat_UIGrayOnly);
            }
            else
            {
                _mat = GameEntry.Util.LoadAsset<Material>(QAssetBundle.Mat_uigray_mat.Mat_UIGray);
            }
            image.material = _mat;
        }

        /// <summary>
        /// 重制图片
        /// </summary>
        /// <param name="image">图片组建</param>
        public static void Reset(this Image image)
        {
            image.material = null;
        }

        /// <summary>
        /// 按钮至灰
        /// </summary>
        /// <param name="button">按钮组建</param>
        /// <param name="includeChilds">是否包含子节点</param>
        /// <param name="needAll">是否全灰</param>
        public static void SetGray(this Button button, bool includeChilds = true, bool needAll = false)
        {
            Material _mat = null;
            if (needAll)
            {
                _mat = GameEntry.Util.LoadAsset<Material>(QAssetBundle.Mat_uigrayonly_mat.Mat_UIGrayOnly);
            }
            else
            {
                _mat = GameEntry.Util.LoadAsset<Material>(QAssetBundle.Mat_uigray_mat.Mat_UIGray);
            }
            if (!includeChilds)
            {
                button.targetGraphic.material = _mat;
            }
            else
            {
                Graphic[] graphics = button.GetComponentsInChildren<Graphic>();
                foreach (var g in graphics)
                {
                    g.material = _mat;
                }
            }
        }

        /// <summary>
        /// 重制按钮
        /// </summary>
        /// <param name="button">按钮组建</param>
        public static void Reset(this Button button, bool includeChilds = true)
        {
            if (!includeChilds)
            {
                button.targetGraphic.material = null;
            }
            else
            {
                Graphic[] graphics = button.GetComponentsInChildren<Graphic>();
                foreach (var g in graphics)
                {
                    g.material = null;
                }
            }
        }
    }
}
